#include "circlelist.h"
#include <stdio.h>
#include <stdlib.h>

typedef struct  _TagCircleList
{
        CircleListNode header;
        CircleListNode* slider; //增加了游标
        int length;
}TagCircleList;

CircleList* CircleList_Create()
{
        TagCircleList* ret = (TagCircleList *)malloc(sizeof(TagCircleList));
        if(ret == NULL){
                return NULL;
        }

        ret->length = 0;
        ret->header.next = NULL;
        ret->slider = NULL;
        return (CircleList *)ret;
}

void CircleList_Destory(CircleList* list)
{
        if(list == NULL){
                return;
        }
        free(list);
        list = NULL;
}

void CircleList_Clear(CircleList* list)
{
        TagCircleList* sList = (TagCircleList *)list;
        if(sList == NULL){
                return;
        }
        sList->length = 0;
        sList->header.next = NULL;
        sList->slider = NULL;
}

int CircleList_Length(CircleList* list)
{
        TagCircleList* sList = (TagCircleList *)list;
        if(sList == NULL){
                return -1;
        }

        return sList->length;
}

int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
{
        int i = 0;
        TagCircleList* sList = (TagCircleList*)list;

        if (list == NULL || node == NULL || pos < 0)
        {
                return -1;
        }
        
        {
                CircleListNode* current = (CircleListNode*) sList;
                for (i = 0; (i < pos) && (current->next != NULL); i++)
                {
                        current = current->next;
                }
                
                node->next = current->next;
                current->next = node;

                //若第一次插入节点 将游标设置好
                if(sList->length == 0){
                        sList->slider = node;
                }

                sList->length++; //长度加1

                //若头插法 current仍然指向头部
                //原因是: 跳0步，没有跳走
                if(current == (CircleListNode*)sList)
                {
                        //获取最后一个元素
                        CircleListNode* last = CircleList_Get(sList, sList->length-1);
                        last->next = current->next;
                }
        }
        return 0;
}

CircleListNode* CircleList_Get(CircleList* list, int pos)
{
        int i=0;
        TagCircleList* sList = (TagCircleList*)list;
        CircleListNode *current = NULL;

        if (list == NULL || pos < 0)
        {
                return NULL;
        }
        current = &sList->header;

        for (i = 0;  (i < pos)  && (current->next != NULL);  i++)
        {
                current = current->next;
        }

        return current->next;
}

CircleListNode* CircleList_Delete(CircleList* list, int pos)
{
        int i = 0;
        TagCircleList* sList = (TagCircleList*)list;
        CircleListNode* ret = NULL;
        CircleListNode* current = NULL;
        CircleListNode* last = NULL;

        if( (sList != NULL) && (pos >= 0) &&(sList->length > 0) ){
                //current = (CircleListNode *)(&(sList->header));
                current = (CircleListNode *)sList;

                for (i = 0; i < pos; i++)
                {
                        current = current->next;
                }

                //若删除第一个元素(头节点)
                if(current = (CircleListNode*)sList){
                        last = (CircleListNode *)CircleList_Get(sList, sList->length-1);
                }

                //求要删除的元素
                ret = current->next;
                current->next = ret->next;

                sList->length--;

                //判断链表是否为空
                if(last != NULL){
                        sList->header.next = ret->next;
                        last->next = ret->next;
                }

                //若删除的元素为游标所指的元素
                if(sList->slider == ret){
                        sList->slider = ret->next;
                }

                //若删除元素后,链表长度为0
                if(sList->length == 0){
                        sList->header.next = NULL;
                        sList->slider = NULL;
                }
        }

        return ret;
}

//add

CircleListNode* CircleList_DeleteNode(CircleList*list, CircleListNode* node)
{
        TagCircleList* sList =  (TagCircleList*)list;
        CircleListNode* ret = NULL;
        CircleListNode* current = NULL;
        int i = 0;

        if(sList != NULL){
                current = (CircleListNode *)sList;

                //查找node在循环链表中的位置
                for (i = 0; i < sList->length; i++){
                        if (current->next == node){
                                ret = current->next;
                                break;
                        }
                        current = current->next;
                }

                //如果ret找到了， 根据i去删除
                if(ret != NULL){
                        CircleList_Delete(sList, i);
                }
        }
        return ret;
}

 //游标相当于SPL中的迭代器 就是指针 指向一个链表的位置
 //重置游标开始位置
CircleListNode* CircleList_Reset(CircleList* list)
{
        TagCircleList* sList =  (TagCircleList*)list;
        CircleListNode* ret = NULL;

        if(sList != NULL){
                sList->slider = sList->header.next;
                ret = sList->slider;
        }

        return ret;
}

//当前的游标
CircleListNode* CircleList_Current(CircleList* list)
{
        TagCircleList* sList =  (TagCircleList*)list;
        CircleListNode* ret = NULL;       

        if(sList != NULL){
                ret = sList->slider;
        }

        return ret;
}

//移动游标
//把当前位置返回,并且游标下移
CircleListNode* CircleList_Next(CircleList* list)
{       
        TagCircleList* sList =  (TagCircleList*)list;
        CircleListNode* ret = NULL;       

        if(sList != NULL && sList->slider != NULL){
                ret = sList->slider;
                sList->slider = ret->next;
        }

        return ret;
}